<% if false # the license inside this if block assertains to this file -%>
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
<% end -%>
def updated_record
  {
    kind: 'dns#resourceRecordSet',
    name: @resource[:name],
    type: @resource[:type],
    ttl: @resource[:ttl].nil? ? 900 : @resource[:ttl],
    rrdatas: @resource[:target]
  }
end

def unwrap_resource(result, resource)
  self.class.unwrap_resource(result, resource)
end

def self.unwrap_resource(result, _resource)
  # DNS service already did server-side filtering.
  result.first
end

<%=
  lines(emit_rubocop(binding, :function, 'prefetch_soa_resource', :disabled))
-%>
def prefetch_soa_resource
  resource = {
    type: 'SOA',
    name: "#{name.split('.').drop(1).join('.')}.",
    managed_zone: @resource[:managed_zone],
    project: @resource[:project],
    credential: @resource[:credential]
  }
  result = fetch_wrapped_resource(resource, 'dns#resourceRecordSet',
                                  'dns#resourceRecordSetsListResponse',
                                  'rrsets')
  if result.nil?
    raise ['Google DNS Managed Zone ', "'#{managed_zone}'",
           'not present in the manifest file'].join(' ')
  end
  result
end
<%= emit_rubocop(binding, :function, 'prefetch_soa_resource', :enabled) -%>
